package com.hn.project.rqinspect.controller;

import cn.hutool.core.util.StrUtil;
import com.hn.common.enums.RoleTypeEnum;
import com.hn.common.utils.CommonUtils;
import com.hn.common.utils.SecurityUtils;
import com.hn.doc.xyj.annotation.Api;
import com.hn.framework.web.controller.BaseController;
import com.hn.framework.web.domain.AjaxResult;
import com.hn.framework.web.page.TableDataInfo;
import com.hn.project.rqinspect.domain.database.BranchCheckpoint;
import com.hn.project.rqinspect.domain.database.BranchPart;
import com.hn.project.rqinspect.domain.database.CheckPartType;
import com.hn.project.rqinspect.domain.dto.BranchCheckpointDto;
import com.hn.project.rqinspect.domain.dto.BranchDto;
import com.hn.project.rqinspect.domain.vo.BranchVo;
import com.hn.project.rqinspect.service.BranchService;
import com.hn.project.rqinspect.service.UserBranchService;
import com.hn.utils.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 网点
 *
 * @author admin
 */
@Api("网点")
@RequestMapping("branch")
@RestController
public class BranchController extends BaseController {

    @Autowired
    private BranchService branchService;

    @Autowired
    private UserBranchService userBranchService;

    @Value("${upload.web.url}")
    private String uploadWebUrl;

    /**
     * 网点简单列表
     */
    @Api(value = "网点简单列表", result = BranchDto.class)
    @GetMapping("listSimple")
    public AjaxResult listSimple(BranchVo branchVo) {
        Integer userId = SecurityUtils.getUserId();
        branchVo.setUserId(userId);
        branchVo.setSimpleFlag(true);
        List<BranchDto> branchDtos = branchService.listBranchDto(branchVo);
        return AjaxResult.success(branchDtos);
    }

    /**
     * 网点列表
     */
    @Api(value = "网点列表", result = BranchDto.class)
    @GetMapping("list")
    public TableDataInfo list(BranchVo branchVo) {
        Integer userId = SecurityUtils.getUserId();
        branchVo.setUserId(userId);
        branchVo.setSimpleFlag(false);

        Integer roleType = branchVo.getRoleType();
        if (RoleTypeEnum.BRANCH.type.equals(roleType) || RoleTypeEnum.ADMIN.type.equals(roleType)) {
            List<Integer> branchIds = userBranchService.listBranchIdsByUserId(userId);
            branchVo.setBranchIds(branchIds);
        }

        startPage();
        List<BranchDto> branchDtos = branchService.listBranchDto(branchVo);
        for (BranchDto branchDto : branchDtos) {
            String imgUrl = branchDto.getImgUrl();
            branchDto.setImgUrl(CommonUtils.pingJieUrl(imgUrl, uploadWebUrl));
            String distanceStr = branchDto.getDistance();
            if(StrUtil.isNotBlank(distanceStr)){
                BigDecimal distance = new BigDecimal(distanceStr);
                branchDto.setDistance(String.valueOf(distance.setScale(3, BigDecimal.ROUND_HALF_UP)));
            }
        }
        return getDataTable(branchDtos);
    }

    /**
     * 网点详情
     */
    @Api(value = "网点详情", result = BranchDto.class)
    @GetMapping("get")
    public AjaxResult get(Integer branchId) {
        BranchDto branchDto = branchService.getBranchDto(branchId);
        if (branchDto != null) {
            String imgUrl = branchDto.getImgUrl();
            branchDto.setImgUrl(CommonUtils.pingJieUrl(imgUrl, uploadWebUrl));
        }
        return AjaxResult.success(branchDto);
    }

    /**
     * 网点部位类型列表
     *
     * @param taskRecordId 任务记录id
     */
    @Api(value = "网点部位类型列表", result = CheckPartType.class)
    @GetMapping("listPartType")
    public AjaxResult listPartType(Integer taskRecordId) {
        AssertUtils.notNull(taskRecordId, "请选择巡检任务");
        List<CheckPartType> checkPartTypes = branchService.listPartType(taskRecordId);
        return AjaxResult.success(checkPartTypes);
    }

    /**
     * 查询网点下的 对应部位类型的 部位列表
     *
     * @param branchId   网点id
     * @param partTypeId 部位类型
     */
    @Api(value = "根据部位类型查询网点下的部位列表", result = BranchPart.class)
    @GetMapping("listBranchPart")
    public AjaxResult listBranchPart(Integer branchId, Integer partTypeId) {
        AssertUtils.notNull(branchId, "请选择所属网点");
        AssertUtils.notNull(partTypeId, "请选择所属部位");
        List<BranchPart> branchParts = branchService.listBranchPart(branchId, partTypeId);
        return AjaxResult.success(branchParts);
    }

    /**
     * 根据网点部位id查询检查点列表
     */
    @Api(value = "根据网点部位id查询检查点列表", result = BranchCheckpoint.class)
    @GetMapping("listBranchCheckpoint")
    public AjaxResult listBranchCheckpoint(Integer branchPartId, Integer taskRecordId) {
        AssertUtils.notNull(branchPartId, "请选择网点部位");
        AssertUtils.notNull(taskRecordId, "请选择巡检任务");
        List<BranchCheckpoint> branchCheckpoints = branchService.listBranchCheckpoint(branchPartId, taskRecordId);
        return AjaxResult.success(branchCheckpoints);
    }

    /**
     * 获取某个部位类型下的未评价的检查点
     */
    @Api(value = "获取未评价的检查点", result = BranchCheckpointDto.class)
    @GetMapping("listNoCheckBranchCheckpoint")
    public AjaxResult listNoCheckBranchCheckpoint(Integer partTypeId, Integer taskRecordId) {
        AssertUtils.notNull(partTypeId, "请选择所属部位");
        AssertUtils.notNull(taskRecordId, "请选择巡检任务");
        return AjaxResult.success(branchService.listNoCheckBranchCheckpoint(partTypeId, taskRecordId));
    }

}
